package com.yanqu.road.dao.impl.activity.jinli;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.jinli.CrossJinLiData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossJinLiDataDaoImpl extends TempDataObjectDao<CrossJinLiData> {

    public static String getCrossJinLiDataSql = "select * from `t_c_jinli_data` where `activityId`=?";

    public Map<Integer, CrossJinLiData> getCrossJinLiDataMap(int activityId) {
        Map<Integer, CrossJinLiData> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossJinLiDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossJinLiData parse = parse(rs);
                map.put(parse.getMallId(), parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String insertCrossJinLiDataSql = "insert into `t_c_jinli_data`(activityId,mallId,luckyNum,totalNum,luckyNumList,isReward,updateTime) values (?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(CrossJinLiData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMallId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLuckyNum()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTotalNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getLuckyNumList())));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isReward()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        if (getDb().executeNoneQuery(insertCrossJinLiDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateCrossJinLiDataSql = "update `t_c_jinli_data` set luckyNum=?,totalNum=?,luckyNumList=?,isReward=?,updateTime=? where activityId=? and mallId=?";

    @Override
    protected boolean updateData(CrossJinLiData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getLuckyNum()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTotalNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getLuckyNumList())));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isReward()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMallId()));
        if (getDb().executeNoneQuery(updateCrossJinLiDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CrossJinLiData parseData(ResultSet rs) throws SQLException {
        CrossJinLiData data = new CrossJinLiData();
        data.setActivityId(rs.getInt("activityId"));
        data.setMallId(rs.getInt("mallId"));
        data.setLuckyNum(rs.getInt("luckyNum"));
        data.setTotalNum(rs.getInt("totalNum"));
        String luckyNumList = rs.getString("luckyNumList");
        if (!StringUtils.isNullOrEmpty(luckyNumList)) {
            data.setLuckyNumList(new ArrayList<>(JSONObject.parseObject(luckyNumList, new TypeReference<List<Integer>>() {
            })));
        }
        data.setReward(rs.getBoolean("isReward"));
        data.setUpdateTime(rs.getLong("updateTime"));
        return data;
    }
}
